﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.BussinesLogic;
using vensoft.Entities;

namespace vensoft.Win32
{
    public partial class frmGestionarProveedor : Form
    {
        IProveedorBL proveedorBL;


        public frmGestionarProveedor()
        {
            InitializeComponent();
            proveedorBL = new ProveedorBL();
        }

        public Int32 estadoBotones { get; set; }

        private void txtBuscarProv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                proveedorBL = new ProveedorBL();
                proveedorBindingSource.DataSource = proveedorBL.GetFromProveedorByDescripcion(txtBuscarProveedor.Text);
            }
        }

        public Int32 Validacion { get; set; }

        private void frmGestionarProveedor_Load(object sender, EventArgs e)
        {
            proveedorBL = new ProveedorBL();
            proveedorBindingSource.DataSource = proveedorBL.GetAllFromProveedor();

            if (estadoBotones == 0)
            {
                btnSeleccionar.Visible = true;
                btnNuevoProv.Visible = false;
                btnEditarProveedor.Visible = false;
                btnEliminarProveedor.Visible = false;
            }
            else
            {
                btnSeleccionar.Visible = false;
                btnNuevoProv.Visible = true;
                btnEditarProveedor.Visible = true;
                btnEliminarProveedor.Visible = true;
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
            Validacion = 1;
        }

        private void btnNuevoProv_Click(object sender, EventArgs e)
        {
            frmAEProveedor aeproveedor = new frmAEProveedor();
            aeproveedor.Nuevo();
            aeproveedor.ShowDialog();
        }

        private void btnActualizarDatos_Click(object sender, EventArgs e)
        {
            proveedorBL = new ProveedorBL();
            proveedorBindingSource.DataSource = proveedorBL.GetAllFromProveedor();
        }

        private void btnEditarProveedor_Click(object sender, EventArgs e)
        {
            frmAEProveedor aeproveedor = new frmAEProveedor();
            aeproveedor.currentProvider = proveedorBindingSource.Current as Proveedor;
            aeproveedor.Editar();
            aeproveedor.ShowDialog();
        }

        public void txtBuscarProveedor_Click(Object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case (MouseButtons.Left):
                    txtBuscarProveedor.Text = null;
                    break;
            }
        }

        private void btnEliminarProveedor_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Estas seguro que deseas eliminar?", "ALERTA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Proveedor currentProvider = proveedorBindingSource.Current as Proveedor;
                proveedorBL.EliminarProveedor(currentProvider);
                proveedorBindingSource.RemoveCurrent();
            }
        }
        private Proveedor _proveedor;
        public Proveedor proveedor
        {
            get { return _proveedor; }
            set { _proveedor = value; }
        }
        private void GetCurrent()
        {
            Proveedor selected = (Proveedor)proveedorBindingSource.Current;

            if (selected != null)
            {
                proveedor = selected;
            }
        }
        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            GetCurrent();
            Close();
        }
        public Proveedor  CurrentProveedor { get; set; }

    }
}

